#include <iostream>
#include <cmath>
#include "rasterization.h"
using namespace std;

#define MY_PI 3.1415926

const TGAColor white = TGAColor(255,255,255,255);
const TGAColor red = TGAColor(255,0,0,255);
const TGAColor green = TGAColor(0,255,0,255);
const TGAColor blue = TGAColor(0,0,255,255);
const TGAColor yellow = TGAColor(255,255,0,255);

Model* model = nullptr;
int width = 200;
int height = 200;
int depth = 255;

vec3 m2v(mat<4,1> m){
    return vec3(m[0][0]/m[3][0], m[1][0]/m[3][0], m[2][0]/m[3][0]);
}

mat<4,1> v2m(vec3 v){
    mat<4,1> m;
    m[0][0] = v.x;
    m[1][0] = v.y;
    m[2][0] = v.z;
    m[3][0] = 1.f;
    return m;
}

mat<4,4> viewport(int x, int y, int h, int w){
    mat<4,4> m = mat<4,4>::identity();
    m[0][3] = x+w/2.f;
    m[1][3] = y+h/2.f;
    m[2][3] = depth/2.f;

    m[0][0] = w/2.f;
    m[1][1] = h/2.f;
    m[2][2] = depth/2.f;
    return m;
}

mat<4,4> translation(vec3 v){
    mat<4,4> m = mat<4,4>::identity();
    m[0][3] = v.x;
    m[1][3] = v.y;
    m[2][3] = v.z;
    return m;
}

mat<4,4> zoom(float factor){
    mat<4,4> Z = mat<4,4>::identity();
    Z[0][0] = Z[1][1] = Z[2][2] = factor;
    return Z;
}

mat<4,4> rotation_x(float cosangle, float sinangle){
    mat<4,4> R = mat<4,4>::identity();
    R[1][1] = R[2][2] = cosangle;
    R[1][2] = -sinangle;
    R[2][1] = sinangle;
    return R;
}

mat<4,4> rotation_y(float cosangle, float sinangle){
    mat<4,4> R = mat<4,4>::identity();
    R[0][0] = R[2][2] = cosangle;
    R[0][2] = sinangle;
    R[2][0] = -sinangle;
    return R;
}

mat<4,4> rotation_z(float cosangle, float sinangle){
    mat<4,4> R = mat<4,4>::identity();
    R[0][0] = R[1][1] = cosangle;
    R[0][1] = -sinangle;
    R[1][0] = sinangle;
    return R;
}

vec3 to_screen(vec3 p){
  return vec3(p[0]*width/2.f/p[2] + width, p[1]*width/2.f/p[2] + height, 1);
}


void draw_original(vec3* verts){
    rasterization rasterization(width, height);
    TGAImage image(width * 2, height * 2, 3);
    // 坐标轴
    vec3 o(0,0,1), x(1,0,1), y(0,1,1);
    vec3 vert[5];
    for(int i = 0; i < 5; i++){
      vert[i] = to_screen(verts[i]);
      std::cout << vert[i][0] << "-" << vert[i][1] << std::endl;

    }
    o = to_screen(o);
    x = to_screen(x);
    y = to_screen(y);
    rasterization.drawALine(o, x, image, red);
    rasterization.drawALine(o, y, image, green);
    for(int i = 0; i < 5; i++){
      rasterization.drawALine(vert[i], vert[(i+1)%5], image,  white); 
    }  

    image.write_tga_file("out/original.tga");
 
}

void draw_transformed(vec3* verts){
  rasterization rasterization(width, height);
  TGAImage image(width * 2, height * 2, 3);
  mat<3,3> trans_mat = mat<3,3>::identity();
  trans_mat[2][0] = -1/5.0f;
  vec3 o(0,0,1), x(1,0,1), y(0,1,1);
  vec3 vert[5];
  for(int i = 0; i < 5; i++){
      vert[i] = trans_mat * verts[i];
      vert[i] = to_screen(vert[i]);
      std::cout << vert[i][0] << "-" << vert[i][1] << std::endl;
  }
  o = to_screen(o);
  x = to_screen(x);
  y = to_screen(y);

  rasterization.drawALine(o, x, image, red);
  rasterization.drawALine(o, y, image, green);
  for(int i = 0; i < 5; i++){
    rasterization.drawALine(vert[i], vert[(i+1)%5], image,  yellow); 
  }  

  image.write_tga_file("out/transformed.tga");
 

}

int main(){

  vec3 verts[5] = {
    vec3(1,0,1),
    vec3(0,1,1),
    vec3(-1,1,1),
    vec3(-1,-1,1),
    vec3(1,-1,1)
  };
  
  draw_transformed(verts);
  draw_original(verts);
  return 0;

}
